package com.artPreter.service.impl;

import com.artPreter.mapper.RegionMapper;
import com.artPreter.pojo.Region;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.artPreter.pojo.Depart;
import com.artPreter.mapper.DepartMapper;
import com.artPreter.pojo.User;
import com.artPreter.service.IDepartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 艺术机构表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2024-11-14
 */
@Service
public class DepartServiceImpl extends ServiceImpl<DepartMapper, Depart> implements IDepartService {

    @Autowired
    private RegionMapper regionMapper;

    @Override
    public List<Depart> getIndexDeparts(int i) {
        List<Depart> result =new ArrayList<>();
        if(i>0){
            LambdaQueryWrapper<Depart> queryWrapper = new LambdaQueryWrapper<>();
            if(i==200){

            }else{
                queryWrapper.eq(Depart::getIsIndex,1);
            }
            queryWrapper.ne(Depart::getIsBase,1);
            queryWrapper.orderByDesc(Depart::getIndexSort);
            queryWrapper.last("limit "+i );
            queryWrapper.eq(Depart::getStatus,1);
            result = this.baseMapper.selectList(queryWrapper);
        }
        return result;
    }

    @Override
    public List<Region> getDepartRegionList(int i) {
        List<Region> result = regionMapper.getDepartRegionList();
        for (int j = 0; j < result.size(); j++) {
            int finalJ = j;
            List<Region> filterList = result.stream().filter(item->item.getId().equals(result.get(finalJ).getPid())).toList();
            if(filterList.isEmpty()){
                LambdaQueryWrapper<Region> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(Region::getId,result.get(finalJ).getPid());
                List<Region> filterList1 = regionMapper.selectList(queryWrapper);
                if(!filterList1.isEmpty()){
                    result.add(filterList1.get(0));
                }
            }
        }
        return result;
    }

    @Override
    public List<Depart> getDepartList(Depart depart) {
        List<Depart> result = new ArrayList<>();
        LambdaQueryWrapper<Depart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Depart::getStatus,1);
        if(depart.getRegion()!=null){
            queryWrapper.eq(Depart::getRegion,depart.getRegion());
        }
        if(depart.getDepartType()!=null&&!depart.getDepartType().equals("")){
            queryWrapper.eq(Depart::getDepartType,depart.getDepartType());
        }
        if(depart.getName()!=null&&!depart.getName().equals("")){
            queryWrapper.like(Depart::getName,depart.getName());
        }
        queryWrapper.orderByDesc(Depart::getSort);
        queryWrapper.ne(Depart::getIsBase,1);
        result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public Depart getDepartById(String deaprtId) {
        if(deaprtId==null||deaprtId.equals("")){
            return null;
        }
        LambdaQueryWrapper<Depart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Depart::getDepartId,deaprtId);
        List<Depart> result = this.baseMapper.selectList(queryWrapper);
        if(result.size()>0){
            return result.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Depart getDepartByInfo(Integer id) {
        Depart depart = this.baseMapper.selectById(id);
        return depart;
    }

    @Override
    public List<Depart> getDepartListWithPid(Depart depart) {
        List<Depart> departList = new ArrayList<>();
        QueryWrapper<Depart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("s.status",1);
        queryWrapper.ne("s.is_base",1);
        if(depart.getDepartType()!=null&&!depart.getDepartType().equals("")){
            queryWrapper.eq("s.depart_type",depart.getDepartType());
        }
        if(depart.getName()!=null&&!depart.getName().equals("")){
            queryWrapper.like("s.name",depart.getName());

        }
        queryWrapper.and(wrappe->wrappe.eq("b.id",depart.getRegion()).or().eq("b.pid",depart.getRegion()));
        departList = this.baseMapper.getDepartListWithPid(queryWrapper);

        return departList;
    }
}
